<?php

namespace App\Http\Controllers\Company;
use App\Facades\Style;
use App\Models\Common\CollArea;
use App\Models\Common\CollPosition;
use App\Http\Requests\Company\CompanySearch\CreateCompanySearchRequest;
use App\Http\Requests\Company\CompanySearch\UpdateCompanySearchRequest;
use App\Models\Company\CompanySearch;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CompanySearchController extends CommonController{
    public function __construct(){
    }

    /**
     * 搜索器列表
     * @return mixed
     */
    public function index(){
        $user = auth()->guard('company')->user();
        $companySearch = CompanySearch::where('uid',$user->uid)->orderBy('updated_at','desc')->paginate(10);
        foreach($companySearch as $k=> $v ){
            $alinks = arrayFilter(array(
                "jobsort1" => !empty($v->jobsort1) ? $v->jobsort1 : '',
                "jobsort2" => !empty($v->jobsort2) ? $v->jobsort2 : '',
                "jobsort3" => !empty($v->jobsort3) ? $v->jobsort3 : '',
                "jobsort4" => !empty($v->jobsort4) ? $v->jobsort4 : '',
                "jobsort5" => !empty($v->jobsort5) ? $v->jobsort5 : '',
                "jobarea1" => !empty($v->jobarea1) ? $v->jobarea1 : '',
                "jobarea2" => !empty($v->jobarea2) ? $v->jobarea2 : '',
                "jobarea3" => !empty($v->jobarea3) ? $v->jobarea3 : '',
                "jobarea4" => !empty($v->jobarea4) ? $v->jobarea4 : '',
                "jobarea5" => !empty($v->jobarea5) ? $v->jobarea5 : '',
                "education" => !empty($v->education) ? $v->education : '',
                "work_year" => !empty($v->work_year) ? $v->work_year : '',
                "age" => !empty($v->age) ? $v->age : '',
                "sex" => !empty($v->sex) ? $v->sex : '',
                "chkphoto_open" => !empty($v->chkphoto_open) ? $v->chkphoto_open : '',
                "homeaddress" => !empty($v->homeaddress) ? $v->homeaddress : '',
                "census" => !empty($v->census) ? $v->census : '',
                "keyword" => !empty($v->keyword) ? $v->keyword : '',
                "updatetime" => !empty($v->updatetime) ? $v->updatetime : '',
            ));
            //设置a连接属性
            $search = '';
            foreach($alinks as $kk => $vv){
                $search .= $kk . '=' . $vv .'&';
            }
            $search = '?'.trim($search,'&');
            $v->search_link = asset('personnel'.$search);
        }
        return Style::view('company.searchIndex',compact('companySearch'));
    }

    /**
     * 添加搜索器
     * @return mixed
     */
    public function create(){
        return Style::view('company.searchCreate');
    }

    /**
     * 添加搜索器提交
     * @param CreateCompanySearchRequest $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(CreateCompanySearchRequest $request){
        $user = auth()->guard('company')->user();
        $input = $request->only(
            'jobsort1','jobsort2','jobsort3','jobsort4','jobsort5',
            'jobarea1','jobarea2','jobarea3','jobarea4','jobarea5',
            'education','work_year','age','sex','chkphoto_open','homeaddress','census',
            'keyword','updatetime','jobsort4','search_name'
        );
        $input['uid'] = $user->uid;
        CompanySearch::create($input);
        return redirect('company/search');
    }

    /**
     * 编辑搜索器
     * @param $id
     * @return mixed
     */
    public function edit($id){
        $companySearch = CompanySearch::find($id);
        //工地地区
        $jobarea1 = ($companySearch['jobarea1']) ?  CollArea::getAreaName($companySearch['jobarea1']) : '';
        $jobarea2 = ($companySearch['jobarea2']) ?  CollArea::getAreaName($companySearch['jobarea2']) : '';
        $jobarea3 = ($companySearch['jobarea3']) ?  CollArea::getAreaName($companySearch['jobarea3']) : '';
        $jobarea4 = ($companySearch['jobarea4']) ?  CollArea::getAreaName($companySearch['jobarea4']) : '';
        $jobarea5 = ($companySearch['jobarea5']) ?  CollArea::getAreaName($companySearch['jobarea5']) : '';
        $companySearch->workplace  = trim($jobarea1 . '+' . $jobarea2 . '+' . $jobarea3 . '+'  . $jobarea4 . '+'  . $jobarea5,'+');
        //工地岗位
        $jobsort1 = ($companySearch['jobsort1']) ?  CollPosition::getPosName($companySearch['jobsort1']) : '';
        $jobsort2 = ($companySearch['jobsort2']) ?  CollPosition::getPosName($companySearch['jobsort2']) : '';
        $jobsort3 = ($companySearch['jobsort3']) ?  CollPosition::getPosName($companySearch['jobsort3']) : '';
        $jobsort4 = ($companySearch['jobsort4']) ?  CollPosition::getPosName($companySearch['jobsort4']) : '';
        $jobsort5 = ($companySearch['jobsort5']) ?  CollPosition::getPosName($companySearch['jobsort5']) : '';
        $companySearch->jobsort  = trim($jobsort1 . '+' . $jobsort2 . '+' . $jobsort3 . '+'  . $jobsort4 . '+'  . $jobsort5,'+');
        return Style::view('company.searchUpdate',compact('companySearch'));
    }

    /**
     * 更新搜索器
     * @param UpdateCompanySearchRequest $request
     * @param $id
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function update(UpdateCompanySearchRequest $request,$id){
        $user = auth()->guard('company')->user();
        $input = $request->only(
            'jobsort1','jobsort2','jobsort3','jobsort4','jobsort5',
            'jobarea1','jobarea2','jobarea3','jobarea4','jobarea5',
            'education','work_year','age','sex','chkphoto_open','homeaddress','census',
            'keyword','updatetime','jobsort4','search_name'
        );
        $result = CompanySearch::where('id',$id)->where('uid',$user->uid)->update($input);
        if($result){
            //flash('更新搜索器成功', 'success');
            return redirect('company/search');
        }else{
            return back()->with('errors','搜索器信息更新失败，请稍后重试！');
        }
    }

    /**
     * 删除搜索器
     * @param Request $request
     * @param $id
     * @return array
     */
    public function destroy(Request $request,$id){
        $user = auth()->guard('company')->user();
        $result = CompanySearch::where('id',$id)->where('uid',$user->uid)->delete();
        if($result){
            $data = [
                'status' => 0,
                'msg' => '搜索器删除成功！',
            ];
        }else{
            $data = [
                'status' => 1,
                'msg' => '搜索器删除失败，请稍后重试！',
            ];
        }
        return $data;
    }

    /**
     * 职位管理
     * @param Request $request
     * @return mixed
     */
    public function message(Request $request){
        $user = auth()->guard('company')->user();
        //获取面试通知记录
        $joblist = DB::table('company_job as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            ->select(
                't1.id','t1.updatetime','t1.com_id','t1.job_id',
                't2.company_name','t1.job_title'
            )
            ->where('t1.uid',$user->uid)
            ->where(function ($query) {
                //$query->where('t3.job_title', 'like', '%'.$this->keyWord.'%')->orWhere('t2.company_name','like','%'.$this->keyWord.'%');
            })
            ->orderBy('created_at')
            ->paginate(20);
        // dd($favorites);
        //设置状态
        //    $job_status =  ($this->job_status!='') ? $this->job_status : '';
        return Style::view('company.interviewManage',compact('joblist'));
    }
}